import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import 'controller/eacher_work_train_course_list_controller.dart';

///教师的培训课程
class TeacherWorkCourseListPage extends StatefulWidget {
  final String tag;

  final bool shrinkWrap;

  const TeacherWorkCourseListPage(
      {Key key, this.shrinkWrap = false, @required this.tag})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TeacherWorkCourseListPageState();
  }
}

class _TeacherWorkCourseListPageState extends State<TeacherWorkCourseListPage>
    with SingleTickerProviderStateMixin {
  TeacherTrainCourseController _historyController;
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _historyController = Get.put(
      TeacherTrainCourseController(),
      tag: widget.tag,
    );
    _tabController = new TabController(length: 2, vsync: this);

    Future.delayed(Duration.zero, () async {
      await _historyController.requestListFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我的培训"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (TeacherTrainCourseController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _historyController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    List<TeacherTrainBean> _list = _historyController.getList();

    if (_list == null || _list.length == 0) {
      return Center(
        child: NoDataWidget(
          text: "暂无参与的培训",
          clickCallBack: () {
            _historyController.requestListFunction(click: true);
          },
        ),
      );
    }
    if (widget.shrinkWrap) {
      return buildListView();
    }
    return RefreshIndicator(
      onRefresh: () async {
        await _historyController.requestListFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: buildListView(),
    );
  }

  AnimatedSwitcher buildListViewAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(
      ///动画执行切换时间
      duration: const Duration(milliseconds: 200),

      ///动画构建器 构建指定动画类型
      transitionBuilder: (Widget child, Animation<double> animation) {
        //执行缩放动画
        return FadeTransition(child: child, opacity: animation);
      },

      ///执行动画的子 Widget
      ///只有子 Widget 被切换时才会触发动画
      child: Container(
        key: UniqueKey(),
        child: buildMyListWidget(),
      ),
    );
  }

  int _currentIndex = 0;

  buildMyListWidget() {
    if (_currentIndex == 0) {
      return buildListView();
    }
    return Padding(
      padding: EdgeInsets.only(left: 10),
    );
  }

  buildListView() {
    List<TeacherTrainBean> _list = _historyController.getList();
    return ListView.builder(
      padding: EdgeInsets.zero,
      shrinkWrap: widget.shrinkWrap,
      physics: widget.shrinkWrap
          ? NeverScrollableScrollPhysics()
          : BouncingScrollPhysics(),
      //条目个数
      itemCount: _list.length,
      //每个子条目的样式
      itemBuilder: (BuildContext context, int index) {
        TeacherTrainBean teacherTrainBean = _list[index];
        return Container(
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          padding: EdgeInsets.all(12),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  Expanded(
                    child: Text(
                      teacherTrainBean.teacherContent,
                      style: TextStyle(
                          fontSize: 14,
                          color: Colors.black,
                          fontWeight: FontWeight.w600),
                    ),
                  )
                ],
              ),
              SizedBox(height: 8),
              Row(
                children: [
                  Text("开始时间:"),
                  Text(
                    "${teacherTrainBean.beginTime} ",
                    style: TextStyle(
                      fontSize: 14,
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  Text("结束时间:"),
                  Text(
                    "${teacherTrainBean.endTime} ",
                    style: TextStyle(
                      fontSize: 14,
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  Text("上课时间:"),
                  Text(
                    "${teacherTrainBean.timeNode} 小节",
                    style: TextStyle(
                      fontSize: 14,
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  Text("使用教材:"),
                  Text(
                    "${teacherTrainBean.teacherBook} ",
                    style: TextStyle(
                      fontSize: 14,
                    ),
                  ),
                ],
              ),
              Container(
                width: double.infinity,
                color: Colors.grey[200],
                padding: EdgeInsets.all(8),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text("培训内容:"),
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                        child: Text("${teacherTrainBean.trainBean.trainName}"))
                  ],
                ),
              )
            ],
          ),
        );
      },
    );
  }
}
